﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;

namespace Gomoku
{
    class GameDraw
    {
        private PictureBox gamemap;
        Graphics g;

        /// <summary>
        /// 构造需要绘制的图片框
        /// </summary>
        /// <param name="GameMap">目标图片框</param>
        public GameDraw(PictureBox GameMap)
        {
            gamemap = GameMap;
        }

        /// <summary>
        /// 绘制初始空白棋盘
        /// </summary>
        /// <param name="Lines">每行每列绘制几条线</param>
        public void Initialize(int Lines)
        {
            //gamemap.Image = new Bitmap(gamemap.Width, gamemap.Height);
            try
            {
                gamemap.Image = Image.FromFile(Application.StartupPath + @"\img\mapbg.jpg");
                g = Graphics.FromImage(gamemap.Image);
            }
            catch (Exception)
            {
                MessageBox.Show("加载背景板失败, 请检查文件是否齐全!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            SolidBrush sb = new SolidBrush(Color.FromArgb(220, 220, 220));
            //g.FillRectangle(sb, new Rectangle(new Point(0, 0), new Size(gamemap.Width, gamemap.Height)));
            //填充背景
            Pen pen = new Pen(Color.Black);
            Point p1 = new Point();
            Point p2 = new Point();
            for (int i = 0; i < Lines; i++)
            {
                p1.X = gamemap.Width / (Lines + 1) * (i + 1);
                p1.Y = gamemap.Height / (Lines + 1);
                p2.X = gamemap.Width / (Lines + 1) * (i + 1);
                p2.Y = gamemap.Height - gamemap.Height / (Lines + 1);
                g.DrawLine(pen, p1, p2);
            }
            //均匀画竖线
            for (int j = 0; j < Lines; j++)
            {
                p1.X = gamemap.Width / (Lines + 1);
                p1.Y = gamemap.Height / (Lines + 1) * (j + 1);
                p2.X = gamemap.Width - gamemap.Width / (Lines + 1);
                p2.Y = gamemap.Height / (Lines + 1) * (j + 1);
                g.DrawLine(pen, p1, p2);
            }
            //均匀画横线
            Rectangle[] rects = new Rectangle[5];
            int a = 640 / (Lines + 1);
            rects[0] = new Rectangle(4 * a - 2, 4 * a - 2, 5, 5);
            rects[1] = new Rectangle(12 * a - 2, 4 * a - 2, 5, 5);
            rects[2] = new Rectangle(8 * a - 2, 8 * a - 2, 5, 5);
            rects[3] = new Rectangle(4 * a - 2, 12 * a - 2, 5, 5);
            rects[4] = new Rectangle(12 * a - 2, 12 * a - 2, 5, 5);
            g.FillRectangles(new SolidBrush(Color.Black), rects);
            //画五个点
            SolidBrush sb2 = new SolidBrush(Color.Black);
            string[] letters = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O" };
            for (int i = 0; i < 15; i++)
            {
                g.DrawString((i + 1).ToString(), new Font("Arial", 14), sb2, new Point(15, 30 + i * 40));
                g.DrawString(letters[i], new Font("Arial", 14), sb2, new Point(33 + i * 40, 15));
            }
            //画数字和字母
            gamemap.Refresh();
        }

        /// <summary>
        /// 绘制目标棋子
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="ChessColor"> 0 - White, 1 - Black</param>
        
        public void DrawChess(int x, int y, int ChessColor)
        {
            Image whiteChess = null, blackChess = null;
            try
            {
                whiteChess = Image.FromFile(Application.StartupPath + @"\img\chess_white.png");
                blackChess = Image.FromFile(Application.StartupPath + @"\img\chess_black.png");
            }
            catch (Exception)
            {
                MessageBox.Show("加载背景板失败, 请检查文件是否齐全!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
            int a = 640 / (GlovalVar.Lines + 1);
            switch (ChessColor)
            {
                case 0:
                    g.DrawImage(whiteChess, new Rectangle(new Point(a / 2 + a * x, a / 2 + a * y), new Size(a, a)));
                    break;
                case 1:
                    g.DrawImage(blackChess, new Rectangle(new Point(a / 2 + a * x, a / 2 + a * y), new Size(a, a)));
                    break;
            }
            gamemap.Refresh();
        }

        public void DrawSuccessLine(ChessPosition Start, ChessPosition End)
        {
            int a = 640 / (GlovalVar.Lines + 1);
            g.DrawLine(new Pen(Color.Red, 2), 
                new Point((Start.X + 1) * a, (Start.Y + 1) * a), new Point((End.X + 1) * a, (End.Y + 1) * a));
            gamemap.Refresh();
        }
    }
}
